option(MNN_CODEGEN_LLVM "Build llvm backend for codegen." OFF)
option(MNN_CODEGEN_C "Build C source backend for codegen." OFF)
option(MNN_CODEGEN_OPENCL "Build OpenCL source backend for codegen." OFF)
option(MNN_CODEGEN_JIT "Build jit for codegen." OFF)


file(GLOB CODEGEN_HEADER "${CMAKE_CURRENT_LIST_DIR}/*.*")
file(GLOB CPU_SRCS "${CMAKE_CURRENT_LIST_DIR}/cpu/*.*")
file(GLOB JIT_SRCS "${CMAKE_CURRENT_LIST_DIR}/jit/*.*")
list(APPEND MNN_CODEGEN_SRCS ${CODEGEN_HEADER})
list(APPEND MNN_CODEGEN_SRCS ${JIT_SRCS})

if(MNN_CODEGEN_OPENCL)
    add_definitions(-DMNN_CODEGEN_OPENCL)
    file(GLOB OPENCL_SRCS "${CMAKE_CURRENT_LIST_DIR}/opencl/*.*")
    list(APPEND MNN_CODEGEN_SRCS ${OPENCL_SRCS})
endif()

if(MNN_CODEGEN_C)
    add_definitions(-DMNN_CODEGEN_CPU)
    add_definitions(-DMNN_CODEGEN_C)
    file(GLOB C_SRCS "${CMAKE_CURRENT_LIST_DIR}/cpu/c/*.*")
    list(APPEND MNN_CODEGEN_SRCS ${CPU_SRCS})
    list(APPEND MNN_CODEGEN_SRCS ${C_SRCS})
endif()

if(MNN_CODEGEN_LLVM)
    add_definitions(-DMNN_CODEGEN_CPU)
    add_definitions(-DMNN_CODEGEN_LLVM)
    file(GLOB LLVM_SRCS "${CMAKE_CURRENT_LIST_DIR}/cpu/llvm/*.*")
    list(APPEND MNN_CODEGEN_SRCS ${CPU_SRCS})
    list(APPEND MNN_CODEGEN_SRCS ${LLVM_SRCS})
    # add llvm libs
    find_package(LLVM REQUIRED CONFIG)
    message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
    message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
    include_directories(${LLVM_INCLUDE_DIRS})
    add_definitions(${LLVM_DEFINITIONS})
    llvm_map_components_to_libnames(llvm_libs core bitwriter OrcJIT Support nativecodegen native CodeGen)
    list(APPEND MNN_EXTRA_DEPENDS ${llvm_libs})
endif()

add_library(MNNCodegen OBJECT ${MNN_CODEGEN_SRCS})
set_property(TARGET MNNCodegen PROPERTY CXX_STANDARD 14)
list(APPEND MNN_OBJECTS_TO_LINK $<TARGET_OBJECTS:MNNCodegen>)
